<?php
    class Avis_admin extends Admin_Controller{
        function Avis_admin(){
            parent::Admin_Controller();
            $this->load->model('Admin_m');
            $this->load->library('pagination');
            $this->load->library('form_validation');
            $this->load->library('session');
        }
        function index(){
            is_admin();
            $this->list_news();
        }
        function add_category(){
            if($this->input->post('submit')==TRUE){
                $cat_name = $this->input->post('cat');
                $this->Admin_m->add_new_category($cat_name);
                redirect('avis_admin/list_categories');   
            }
        }
        function update_cat($id){
            is_admin();
            $this->_data['query']=$this->Admin_m->get_cat_by_id($id);
            if($this->input->post('submit'))
            {    
                $data = array(
                'name'=>$this->input->post('txtCat')
                );
                $this->Admin_m->update_cat($id,$data);
                redirect(site_url("avis_admin/list_categories"));
            }
            else
            {
                $this->_load_view('admin/cat_update');
            }
        }
        function delete_cat($id,$per_page,$off_set){
            is_admin();
            $query=$this->Admin_m->get_news_by_cat($id);
            foreach($query as $row){
                $this->delete_news_action($row['id']);
            }
            $this->Admin_m->delete_cat($id);
            redirect('avis_admin/list_categories/'.$per_page.'/'.$off_set);
        }
        /*Category*/
        function list_categories(){
            is_admin();
            $number_per_page =  $this->uri->segment(3);
            if($number_per_page=='')
            {
                $number_per_page = 15;
            }
            $offset = $this->uri->segment(4);
            if($offset ==''){
                $offset = 0;
            }
            $config['base_url'] = base_url().'index.php/avis_admin/list_categories/'.$number_per_page.'/';
            $config['uri_segment'] = 4;
            $config['per_page']='15';

            $config['total_rows'] = $this->db->count_all('categories');
            if($config['total_rows']==$offset&&$offset>0){
                $offset=$offset-$number_per_page;
            } 
            $this->_data['query'] = $this->Admin_m->get_all_categories($number_per_page,$offset);
            $this->pagination->initialize($config);  
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['per_page']= $number_per_page;
            $this->_data['off_set']= $offset;
            $this->_data['page_title']="category list";
            $this->_load_view('admin/categories');  
        }

        function list_news(){
            is_admin();
            $number_per_page =  $this->uri->segment(3);
            if($number_per_page=='')
            {
                $number_per_page = 4;
            }
            $offset = $this->uri->segment(4);
            if($offset ==''){
                $offset = 0;
            }
            $config['base_url'] = base_url().'index.php/avis_admin/list_news/'.$number_per_page.'/';
            $config['uri_segment'] = 4;
            $config['per_page']='4';

            $config['total_rows'] = $this->db->count_all('news');
            if($config['total_rows']==$offset&&$offset>0){
                $offset=$offset-$number_per_page;    
            } 
            $this->_data['query'] = $this->Admin_m->get_all_news($number_per_page,$offset);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['page_title']="List news";
            $this->_data['per_page']= $number_per_page;
            $this->_data['off_set']= $offset;
            $this->_load_view('admin/news');   
        }
        function post_news(){
            is_admin();
            $this->_data['error'] = '';
            $all = $this->db->count_all('categories');
            $this->_data['categories']=$this->Admin_m->get_all_categories($all,0);
            $this->_load_view('admin/post_news');
        }
        function update_news($id){
            is_admin();
            $query = $this->Admin_m->get_News_By_ID($id);
            if($query->num_rows<1){
                redirect('avis_admin/list_news');
            }
            $this->_data['query'] = $query;
            if($this->input->post('submit')){
                $image_path = './assets/news_img';
                $thumb_path = './assets/thumbs';
                $config = array(
                'allowed_types' => "jpg|jpeg|gif|png",
                'upload_path' => $image_path,
                'max_size'=>4000
                );
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload())
                {
                    $data = array(
                    'title'=>$this->input->post('titre'),
                    'content'=>$this->input->post('txtDes'),
                    'date_post'=>time(),
                    'author'=>$this->session->userdata('admin'),
                    'cat'=>$this->input->post('categorie') 
                    );
                    $this->Admin_m->update_news($data,$id);
                    redirect(site_url('avis_admin/list_news'));
                }    
                else
                {
                    $query = $this->Admin_m->get_News_By_ID($id);
                    if($query->num_rows()>0){
                        $info = $query->row();
                        $image_to_delete = './assets/news_img/'.$info->image;
                        $thumb_to_delete = './assets/thumbs/'.$info->image;
                        delete_image($image_to_delete,$thumb_to_delete);
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                    'source_image' => $upload_info['full_path'], //get original image
                    'new_image' => $thumb_path, //save as new image //need to create thumbs first
                    'maintain_ratio' => true,
                    'width' => 300,
                    'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config 
                    $data = array(
                    'title'=>$this->input->post('titre'),
                    'content'=>$this->input->post('txtDes'),
                    'date_post'=>time(),
                    'author'=>$this->session->userdata('admin'),
                    'image'=>$upload_info['file_name'],
                    'cat'=>$this->input->post('categorie') 
                    );
                    $this->Admin_m->update_news($data,$id);
                    redirect(site_url('avis_admin/list_news'));
                }
            } else{
                $this->_data['error'] = '';
                $this->_load_view('admin/update_news');
            }    
        }
        function delete_news($id,$per_page,$off_set){
            $this->delete_news_action($id);
            redirect(site_url('avis_admin/list_news/'.$per_page.'/'.$off_set));
        }
        function delete_news_action($id){
            $query = $this->Admin_m->get_News_By_ID($id);
            if($query->num_rows()>0){
                $info = $query->row();
                $image_to_delete = './assets/news_img/'.$info->image;
                $thumb_to_delete = './assets/thumbs/'.$info->image;
                $row_affect = $this->Admin_m->delete_news($id);
                delete_image($image_to_delete,$thumb_to_delete);
            }
        }
        function upload_picture(){
            is_admin();
            $image_path = './assets/news_img';
            $thumb_path = './assets/thumbs';
            $config = array(
            'allowed_types' => "jpg|jpeg|gif|png",
            'upload_path' => $image_path,
            'max_size'=>2000
            );
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload())
            {
                $this->_data['error'] = $this->upload->display_errors();
                $this->_load_view('admin/post_news');
            }    
            else
            {
                $upload_info = $this->upload->data();
                $config = array(
                'source_image' => $upload_info['full_path'], //get original image
                'new_image' => $thumb_path, //save as new image //need to create thumbs first
                'maintain_ratio' => true,
                'width' => 300,
                'height' => 200
                );
                $this->load->library('image_lib', $config); //load library
                $this->image_lib->resize(); //do whatever specified in config 
                $data = array(
                'title'=>$this->input->post('titre'),
                'content'=>$this->input->post('txtDes'),
                'date_post'=>mdate("%Y-%m-%d",time()),
                'author'=>$this->session->userdata('admin'),
                'image'=>$upload_info['file_name'],
                'cat'=>$this->input->post('categorie') 
                );
                $this->Admin_m->post_news_data($data);
                redirect(site_url('avis_admin/list_news'));
                //print_r($data);
            }
        }
        /*User module*/
        function list_users(){
            is_admin();
            $num_per_page = $this->uri->segment(3);
            if($num_per_page==''){
                $num_per_page = 10; 
            }
            $off_set = $this->uri->segment(4);
            if($off_set==''){
                $off_set=0; 
            }
            $config['base_url']=base_url().'index.php/avis_admin/list_users/'.$num_per_page.'/';
            $config['per_page'] = '10';
            $config['uri_segment']=4;
            $config['total_rows'] = $this->db->count_all('user');
            $this->_data['query']= $this->Admin_m->get_all_user($config['per_page'],$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['per_page']= $num_per_page;
            $this->_data['off_set']= $off_set;
            $this->_data['page_title']="List news";
            $this->_load_view('admin/list_users');
        }
        function delete_user($id){
            is_admin();
            $this->Admin_m->delete_user($id);
            redirect('avis_admin/list_users');
        }
        function add_user(){
            is_admin();
            $conregis = array(
            array('field' => 'firstname',
            'label' => 'First name',
            'rules' => 'required' 
            ),
            array('field' => 'lastname',
            'label' => 'Last name',
            'rules' => 'required' 
            ),
            array('field' => 'email',
            'label' => 'Email',
            'rules' => 'required|valid_email' 
            ),
            array('field' => 'username',
            'label' => 'User name',
            'rules' => 'required' 
            ),
            array('field' => 'password',
            'label' => 'Password',
            'rules' => 'required|matches[confirmpassword]' 
            ),
            array('field' => 'confirmpassword',
            'label' => 'Confirm password',
            'rules' => 'required' 
            ),
            );
            $this->form_validation->set_rules($conregis);
            if($this->input->post('register')){
                if($this->form_validation->run() == TRUE){
                    $data = array(
                    'username'=>$this->input->post('username'),
                    'password'=>md5($this->input->post('password')),
                    'first_name'=>$this->input->post('firstname'),
                    'last_name'=>$this->input->post('lastname'),
                    'email'=>$this->input->post('email')
                    );
                    $this->Admin_m->add_user($data);
                    redirect('avis_admin/list_users');
                }else{
                    $this->_load_view('admin/add_user');   
                }
            }else{
                $this->_load_view('admin/add_user');
            }
        }
        function update_user($id){
            is_admin();
            $config = array(
            array('field' => 'firstname',
            'label' => 'First name',
            'rules' => 'required' 
            ),
            array('field' => 'lastname',
            'label' => 'Last name',
            'rules' => 'required' 
            ),
            array('field' => 'email',
            'label' => 'Email',
            'rules' => 'required|valid_email' 
            )
            );
            $this->form_validation->set_rules($config);
            $this->_data['query'] = $this->Admin_m->get_user_by_id($id);
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){                    
                    $data = array(
                    'first_name'=>$this->input->post('firstname'),
                    'last_name'=>$this->input->post('lastname'),
                    'email'=>$this->input->post('email')
                    );
                    $this->Admin_m->update_user($data,$id);
                    redirect('avis_admin/list_users');
                } else{
                    echo $this->input->post('email'); 
                    $this->_load_view('admin/update_user');
                }    
            } else{
                $this->_load_view('admin/update_user');
            }
        }
        /*Administrator*/
        function list_admins(){
            is_super_admin();
            $num_per_page = $this->uri->segment(3);
            if($num_per_page==''){
                $num_per_page = 10;
            }
            $off_set = $this->uri->segment(4);
            if($off_set==''){
                $off_set=0;
            }
            $config['base_url']=base_url().'index.php/avis_admin/list_admins/'.$num_per_page.'/';
            $config['uri_segment']=4;
            $config['per_page'] = '3';
            $config['total_rows']=$this->db->count_all('admins');
            $this->_data['query'] = $this->Admin_m->get_all_admin($num_per_page,$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination']=$this->pagination->create_links();
            $this->_load_view('admin/list_admins'); 
        }
        function add_admin(){
            is_super_admin();
            $config = array(
            array(
            'field'   => 'ident',
            'label'   => 'ident',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'pass',
            'label'   => 'Passsssssss',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'last_name',
            'label'   => 'last_name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'nom',
            'label'   => 'nommmmmm',
            'rules'   => 'required'
            )
            );
            $this->form_validation->set_rules($config); 
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){
                    $data = array(
                    'name'=>$this->input->post('ident'),
                    'pass'=>md5($this->input->post('pass')), 
                    'first_name'=>$this->input->post('nom'), 
                    'last_name'=>$this->input->post('last_name'),
                    'level'=>2 
                    ); 
                    $this->Admin_m->add_admin($data);
                    redirect('avis_admin/list_admins');   
                } else{
                    $this->_load_view('admin/new_admin');    
                }
            } else{
                $this->_load_view('admin/new_admin');
            }
        }
        function update_admin($id){
            is_super_admin();
            $config = array(
            array(
            'field'   => 'ident',
            'label'   => 'ident',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'last_name',
            'label'   => 'last_name',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'nom',
            'label'   => 'nommmmmm',
            'rules'   => 'required'
            )
            );
            $this->form_validation->set_rules($config);
            $this->_data['query'] = $this->Admin_m->get_admin_by_ID($id);
            if($this->input->post('submit')){
                if($this->form_validation->run()==TRUE){
                    $data = array(
                    'name'=>$this->input->post('ident'),
                    'first_name'=>$this->input->post('nom'),
                    'last_name'=>$this->input->post('last_name')    
                    );
                    $this->Admin_m->update_admin($data,$id);
                    redirect(site_url('avis_admin/list_admins'));

                } else{
                    $this->_load_view('admin/update_admin');
                }
            } else{
                $this->_load_view('admin/update_admin');
            }
        }
        function delete_admin($id){
            is_super_admin();
            $this->Admin_m->delete_admin($id);
            redirect('avis_admin/list_admins');
        }
        /*End of administrator module*/
        function login(){
            $this->form_validation->set_rules('ident', 'Username', 'required');
            $this->form_validation->set_rules('pass', 'Password', 'required');
            //$this->form_validation->set_error_delimiters('<p class="not_error"><span class="img"></span>','<span class="close"></span></p>');
            if($this->input->post('submit')){
                //thuc hien validate form va check csdl
                if ($this->form_validation->run()){
                    $name = $this->input->post('ident');
                    $pass = $this->input->post('pass');
                    $data=array(
                    'name'=>$name,
                    'pass'=>md5($pass)
                    );
                    $query=$this->Admin_m->login($data);
                    if ($query->num_rows()>0){
                        $admin_data=$query->row();
                        $this->session->set_userdata('adminid',$admin_data->id);
                        $this->session->set_userdata('adminname',$admin_data->first_name.' '.$admin_data->last_name);
                        $this->session->set_userdata('level',$admin_data->level);
                        redirect('avis_admin');
                    } else{
                        $this->_data['error'] = 'Nom d\'utilisateur incorrect ou mot de passe';
                        $this->_load_view('admin/login');
                    }

                }
                else {
                    $this->_load_view('admin/login');
                }
            }
            else {
                //hien thi trang admin
                $this->_load_view('admin/login');
            }

        }

        function log_out(){
            $this->session->sess_destroy();
            redirect('avis_admin/login');
        }
        function edit_profile($id){
            is_admin();
            $this->form_validation->set_rules('first_name', 'first_name','required');
            $this->form_validation->set_rules('last_name', 'last_name', 'required');
            $this->_data['query']=$this->Admin_m->get_admin_by_ID($id);
            if($this->input->post('submit')){
                //validate form va thay profile
                if ($this->form_validation->run()==TRUE){
                    $data = array(
                    'last_name'=>$this->input->post('last_name'),
                    'first_name'=>$this->input->post('first_name')
                    );
                    $this->Admin_m->update_profile($data,$id);
                    $this->session->set_userdata('adminname',$data['first_name'].' '.$data['last_name']);
                    redirect('avis_admin');
                }
                else {
                    $this->_load_view('admin/edit_profile');
                }
            }
            else {
                $this->_load_view('admin/edit_profile');
            }
        }
        function change_pass($id){
            is_admin();
            $this->form_validation->set_rules('old_pass', 'old_password','required');
            $this->form_validation->set_rules('new_pass', 'Password', 'required|matches[conf_pass]');
            $this->form_validation->set_rules('conf_pass', 'confirmation_password', 'required');
            if($this->input->post('submit')){
                //validate form va thay pass
                if ($this->form_validation->run()==TRUE){
                    $old_pass= md5($this->input->post('old_pass'));
                    //print_r($this->Admin_m->is_password_match($id,$old_pass));
                    if ($this->Admin_m->is_password_match($id,$old_pass)==TRUE) {
                        $new_pass=md5($this->input->post('new_pass'));
                        //thuc hien lenh doi password
                        $this->Admin_m->change_pass($id,$new_pass);
                        $this->_data['info'] = 'your password has been changed';
                        $this->_load_view('admin/change_pass');
                    }
                    else {
                        $this->_data['info'] = 'your old password does not match';
                        $this->_load_view('admin/change_pass');
                    }

                }
                else {
                    $this->_load_view('admin/change_pass');
                }
            }
            else {$this->_load_view('admin/change_pass');}
        }
        function site_info($setting_name='summary'){
            $this->_data['site_settings'] = $this->Admin_m->get_all_site_info();
            $this->_data['settings'] = $this->Admin_m->get_setting_info($setting_name);
            $this->_load_view('admin/site_info');
        }
        function save_settings($setting_name){
            $data = $this->input->post('txtSettings');
            $this->Admin_m->save_settings($data,$setting_name);
            redirect(site_url('avis_admin/site_info').'/'.$setting_name);
        }
    }

?>
